﻿/****************************************************************************
* Copyright (c) 2008, j. montgomery, ATGi                                   *
* All rights reserved.                                                      *
*                                                                           *
* Redistribution and use in source and binary forms, with or without        *
* modification, are permitted provided that the following conditions        *
* are met:                                                                  *
*                                                                           *
* - Redistributions of source code must retain the above copyright          *
*   notice, this list of conditions and the following disclaimer.           *
*                                                                           *
* - Redistributions in binary form must reproduce the above                 *
*   copyright notice, this list of conditions and the following             *
*   disclaimer in the documentation and/or other materials provided         *
*   with the distribution.                                                  *
*                                                                           *
* - Neither the name of ATGi nor the names of its contributors may          *
*   be used to endorse or promote products derived from this software       *
*   without specific prior written permission.                              *
*                                                                           *
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS       *
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT         *
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS         *
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE            *
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,       *
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES                  *
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR        *
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)        *
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,       *
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)             *
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED       *
* OF THE POSSIBILITY OF SUCH DAMAGE.                                        *
*****************************************************************************
*/
using System;
using System.Collections.Generic;
using System.Text;

using DnSmtp;
using DnSmtp.Message;

namespace SampleCode
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create the Message
            MailMessage message = new MailMessage();
            message.ToAddresses.Add(new EmailAddress("john@gmail.com", "Mr. John Q. Public"));
            message.CcAddresses.Add(new EmailAddress("bob@hotmail.com", "Bob Masters"));
            message.FromAddress = new EmailAddress("susie@gmail.com", "Susie Smith");
            message.Subject = "Testing...";

            // Several Ways to add a message
            
            // Add a specific Mime type
            message.Items.Add(new MessageContent(MimeType.MessageRfc822, "This is a test message."));

            // Help Methods to just add HTML directly
            message.AddHtmlMessage("<B>test</b><br/>\r\nlater,\r\n<br/>susie");

            // The Mail object can be constructed to deliver mail in a few different ways.
            // 1. Directly to the USER's SMTP server registered via DNS (MX Record lookup) (DeliveryMethod.Direct)
            // 2. Or relayed out through your local Mail server (DeliveryMethod.Relay)
            // 
            // If you provide no constructor parameters, it will attempt to detect your 
            // DNS Server by extracting the first DNS serer from your Active NIC's registered 
            // DNS server and attempt to deliver all messages using the Direct DNS method
            // 
            // Otherwise, you can configure the Mail object by providing the IP address of the 
            // local SMTP relay server for relay delivery, or the IP address the local DNS server 
            // for direct delivery.
            Mail m = new Mail();

            try
            {
                m.Send(message);
            }
            catch (RecipientFailedException rfe)
            {
                // Loop over all the failed recipients in the To, CC, Bcc lists.
                foreach (FailedRecipient recipient in rfe.FailedRecipients)
                {
                    Console.WriteLine("Recipient failed " + recipient.FailedAddress);
                    PrintExceptions(recipient.Exception);
                }
            }

            Console.ReadLine();
        }

        static void PrintExceptions(Exception e)
        {
            if (e != null)
            {
                Console.WriteLine(e.Message);

                PrintExceptions(e.InnerException);
            }
        }
    }
}
